package com.ywu.application.standard.redisCache.service;

import com.ywu.boot.utils.RedisUtils;
import com.ywu.common.result.CommonResult;
import com.ywu.application.standard.redisCache.dto.RedisCacheDto;
import com.ywu.application.standard.redisCache.entity.HashEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.connection.DataType;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName RedisExecService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2025/3/22 16:21
 * @Version 1.0
 **/
@Service
public class RedisExecService {

    /**
    * @Author GroundDemo
    * @Description 查询缓存信息
    * @Date 16:22 2025/3/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryRedisCache(RedisCacheDto dto) {
        // 获取键的类型
        DataType dataType = RedisUtils.geKeyType(dto.getRedisKey());
        // 如果类型为none，表示键不存在
        switch (dataType) {
            case NONE:
                return CommonResult.success(null);
            case STRING:
                String redisInfo = RedisUtils.getByKey(dto.getRedisKey());
                return CommonResult.success(redisInfo);
            case HASH:
                if (StringUtils.isEmpty(dto.getRedisHashKey())) {
                    Map<Object, Object> allHashInfo = RedisUtils.getAllHashInfo(dto.getRedisKey());
                    Set<Object> keys = allHashInfo.keySet();
                    List<HashEntity> res = new ArrayList<>();
                    for (Object key : keys) {
                        String keyStr = (String) key;
                        HashEntity hashEntity = new HashEntity();
                        hashEntity.setKey(keyStr);
                        hashEntity.setValue(allHashInfo.get(key));
                        res.add(hashEntity);
                    }
                    return CommonResult.success(res);
                } else {
                    String hashInfo = RedisUtils.getHashByKey(dto.getRedisKey(), dto.getRedisHashKey());
                    return CommonResult.success(hashInfo);
                }
            default:
                break;
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 删除缓存
    * @Date 18:08 2025/3/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteRedisCache(RedisCacheDto dto) {
        if (dto.getRedisKey().endsWith("*")) {
            return doDeleteKeys(dto);
        }
        // 获取键的类型
        DataType dataType = RedisUtils.geKeyType(dto.getRedisKey());
        // 如果类型为none，表示键不存在
        switch (dataType) {
            case NONE:
                return CommonResult.success(null);
            case STRING:
                RedisUtils.deleteByKey(dto.getRedisKey());
                return CommonResult.success(null);
            case HASH:
                if (StringUtils.isEmpty(dto.getRedisHashKey())) {
                    // 删除整个hash键值
                    RedisUtils.deleteByKey(dto.getRedisKey());
                } else {
                    RedisUtils.deleteHashKey(dto.getRedisKey(), dto.getRedisHashKey());
                }
                return CommonResult.success(null);
            default:
                break;
        }
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 批量删除
    * @Date 19:31 2025/3/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    private CommonResult<Object> doDeleteKeys(RedisCacheDto dto) {
        Set<String> keys = RedisUtils.getKeys(dto.getRedisKey());
        RedisUtils.deleteByKeys(keys);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 添加redis缓存
    * @Date 18:41 2025/3/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> addRedisCache(RedisCacheDto dto) {

        return null;
    }

    /**
    * @Author GroundDemo
    * @Description 批量删除
    * @Date 19:37 2025/3/22
    * @Param [dto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteRedisCacheBatch(RedisCacheDto dto) {
        if (CollectionUtils.isEmpty(dto.getBatchRedisKey())) {
            return CommonResult.success(null);
        }
        List<String> batchRedisKey = dto.getBatchRedisKey();
        for (String redisKey : batchRedisKey) {
            RedisCacheDto redisCacheDto = new RedisCacheDto();
            redisCacheDto.setRedisKey(redisKey);
            deleteRedisCache(redisCacheDto);
        }
        return CommonResult.success(null);
    }
}
